#!/bin/sh
#校验顺序
# 【1】eslint检查
# 【2】prettier格式化
# 【3】css校验

# 输出颜色函数
printColor() {
  # 红色
  if [ "$1" = "red" ]
  then
    echo -e "\033[31m $2 \033[0m"
  # 绿色
  elif [ "$1" = "green" ]
  then
    echo -e "\033[32m $2 \033[0m"
  # 黄色
  elif [ "$1" = "yellow" ]
  then
    echo -e "\033[33m $2 \033[0m"
  # 蓝色
  elif [ "$1" = "blue" ]
  then
    echo -e "\033[34m $2 \033[0m"
  # 黑底白字
  elif [ "$1" = "backwhite" ]
  then
    echo -e "\033[40;37m $2 \033[0m"
  # 白底蓝字
  elif [ "$1" = "whiteblue" ]
  then
    echo -e "\033[47;34m $2 \033[0m"
    # 红底白字
  elif [ "$1" = "redwhite" ]
  then
    echo -e "\033[41;37m$2\033[0m"
  # 白色
  else 
    echo -e "\033[37m $2 \033[0m"
  fi
}

#校验所有必须安装的依赖
which prettier &
>/dev/null
if [[ "$?" == 1 ]]; then
  echo -e "\033[41mPlease install prettier -----  npm install -g prettier \033[0m"
  exit 1
fi

# 检验是否全局安装eslint
which eslint &
>/dev/null
if [[ "$?" == 1 ]]; then
  echo -e "\033[41mPlease install eslint -----  npm install -g prettier \033[0m"
  exit 1
fi

# 检查eslint安装版本（需要安装6.8.0） 检测prettier安装版本（需要安装1.19.1）
node hooks/versionLint.js
if [[ "$?" == 1 ]]; then
  exit 1
fi

# 检查新官网框架端口和进程名称
node hooks/fileLint.js
if [[ "$?" == 1 ]]; then
  exit 1
fi

# ============ eslint校验[begin] ============
STAGE_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.vue')
if test ${#STAGE_FILES} -gt 0; then
  echo '开始eslint检查'

  which eslint &
  >/dev/null
  if [[ "$?" == 1 ]]; then
    echo '没安装eslint'
    exit 1
  fi

  PASS=true

  for FILE in $STAGE_FILES; do
    eslint "$FILE" --fix
    if [[ "$?" == 1 ]]; then
      PASS=false
    fi
  done

  if ! $PASS; then
    echo "eslint检查没通过！"
    exit 1
  else
    echo "eslint检查完毕"
  fi

else
  echo '没有vue文件需要检查'
fi
# ============ eslint校验[end] ============

# ============ vueLint校验[begin] ============
STAGE_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.vue')
if test ${#STAGE_FILES} -gt 0; then
  echo '开始vueLint检查'

  PASS=true

  for FILE in $STAGE_FILES; do
    node hooks/vueLint.js "$FILE"
    if [[ "$?" == 1 ]]; then
      PASS=false
    fi
  done

  if ! $PASS; then
    echo "vueLint检查没通过！"
    exit 1
  else
    echo "vueLint检查完毕"
  fi

else
  echo '没有vue文件需要检查'
fi
# ============ vueLint校验[end] ============

# ============ 存在js代码的文件校验[start] ============
jsfilesvalidate=$(git diff --cached --name-only --diff-filter=ACM -- '*.js' '*.vue' '.html')
    echo 开始校验js代码
    echo $jsfilesvalidate
    k=0
    for FILE in $jsfilesvalidate
    do
        node hooks/jslint.js --file=$FILE
        if [[ "$?" == 1 ]]; then
      k=`expr $k + 1`
    fi
    done
    if [[ "$k" == "0" ]]; then
        echo js代码校验通过
        # exit 0
    else 
        echo 存在"$k"个文件js校验失败
        exit 1
    fi
# ============ 存在js代码的文件校验[end] ============

# ============ prettier格式化[begin] ============
jsfiles=$(git diff --cached --name-only --diff-filter=ACM -- '*.vue' 'src/store/*.js' 'src/api/*.js')
if [[ "$jsfiles" != "" ]]; then
  echo -e "\nFormatting Javascript:\n"
  FORMATE=true

  # Check for prettier
  which prettier &
  >/dev/null
  if [[ "$?" == 1 ]]; then
    echo -e "\033[41mPlease install prettier\033[0m"
    exit 1
  fi

  # Prettify all staged .vue|.js files
  for FILE in $jsfiles; do
    isMinFile=$(echo -e "$FILE" | grep ".min.")

    if [[ "$isMinFile" == "" ]]; then
      echo -e "$FILE" | xargs prettier --print-width 80 --no-semi false --single-quote true --write --html-whitespace-sensitivity ignore && git add "$FILE"
      if [[ "$?" == 0 ]]; then
        echo -e "\033[32mprettier Passed: $FILE\033[0m"
      else
        echo -e "\033[41mprettier Failed: $FILE\033[0m"
        FORMATE=false
      fi
    fi
  done

  echo -e "\nJavascript formatting completed!\n"

  if ! $FORMATE; then
    echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass prettier but do not. Please fix the prettier errors and try again.\n"
    exit 1
  else
    echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n"
  fi
fi
# ============ prettier格式化[end] ============

# ============ css校验[start] ============
STAGE_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.vue')
echo 开始校验CSS代码
k=0
for FILE in $STAGE_FILES; do
  node hooks/lint.js --file=$FILE
  if [[ "$?" == 1 ]]; then
    k=$(expr $k + 1)
  fi
done
if [[ "$k" == "0" ]]; then
  echo CSS代码校验通过
else
  echo 存在"$k"个文件CSS校验失败
  exit 1
fi
# ============ css校验[end] ============

# ============ vue/watch校验[start] ============
STAGE_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.vue')
echo 开始校验vue/watch配置项代码
k=0
for FILE in $STAGE_FILES; do
  node hooks/validateWatch.js --file=$FILE
  if [[ "$?" == 1 ]]; then
    k=$(expr $k + 1)
  fi
done
if [[ "$k" == "0" ]]; then
  echo vue/watch配置校验通过
else
  echo 存在"$k"个文件vue/watch配置项校验失败
  exit 1
fi
# ============ vue/watch校验[end] ============

# ============ 公共文件变动邮件推送 ============
printColor green "\n====== 公共文件代码变动校验开始 ======\n"
node hooks/commonFileChangeReport.js
if [[ "$?" == 1 ]]; then
  echo -e "\033[41mcommonFileChangeReport.js 脚本执行失败\033[0m"
  exit 1
fi
# ============ 公共文件变动邮件推送 ============

exit 0
